<%--
    Author      :   Usman Malik
    Date        :   14 Sep 2009
    Purpose     :   Display the content
--%>

<%@ page import="java.lang.reflect.Method " %>
<%@ page import="org.apache.log4j.Logger" %>
<% final Logger log = Logger.getLogger("JSP.CONTENTPAGE"); %>

<%@ include file="../includes/header.jspf" %>

<div style="padding-top: 10px;background-image:url('../images/new/top_bar.jpg');background-repeat: repeat-x;background-position:0% 350%;" title="" align="center">
        <a href="<%=context_Path%>/index.jsp"><img src='<%=context_Path%>/imgsrv?img=home<%=sImgFix%>.jpg&repo=PI&s=0&w=<%=mainButtonImgWidth%>' alt="home"></a>
        <a href="<%=context_Path%>/content"><img src='<%=context_Path%>/imgsrv?img=downloads_selected<%=sImgFix%>.jpg&repo=PI&s=0&w=<%=mainButtonImgWidth%>' alt="downloads"></a>
        <a href="<%=context_Path%>/coolstuff/index.jsp"><img src='<%=context_Path%>/imgsrv?img=cool_stuff<%=sImgFix%>.jpg&repo=PI&s=0&w=<%=mainButtonImgWidth%>' alt="coolstuff"></a>
        <a href="<%=context_Path%>/news/news.jsp"><img src='<%=context_Path%>/imgsrv?img=news<%=sImgFix%>.jpg&repo=PI&s=0&w=<%=mainButtonImgWidth%>' alt="news"></a>
    </div>
<%!
    static final int intBlockLength = 5;
    public int intContentLength = 12;
%>

<%

thisPage = "ContentPage";

    String logtag = "", strCatID = "";
    boolean isNext = false;
    int intPageID=0;
    int intBlock =0;
    int intNextBlock=0;
    int intPrevousPage=0;
    int intNextPage=0;
    int intPrvBlock =0;
    int intCountCont=0;
    
    String nav_Extra = ""; // To show extra items in Navigation in Content Page
    boolean showTopLinks = false; //weather to show the top Main Category selection links
    
    try {
        strCatID = ( request.getParameter("CatID").trim() ); 
        try{strAppMainCat = request.getParameter("MainCat").trim();}catch(Exception ex){strAppMainCat = "";}
        try{strAppCatType = request.getParameter("CatType").trim();}catch(Exception ex){strAppCatType = "";}
        try{strAppFOrS= request.getParameter("FOrS").trim();}catch(Exception ex){strAppFOrS = "";}
        
        if(strAppMainCat.equalsIgnoreCase("islamic") || strAppMainCat.equalsIgnoreCase("bollywood")
                || strAppMainCat.equalsIgnoreCase("lollywood") || strAppMainCat.equalsIgnoreCase("hollywood")) {
            showTopLinks = true;
        }
        
        if(strAppCatType.equalsIgnoreCase("wallpapers") || strAppCatType.equalsIgnoreCase("animated")
                || strAppCatType.equalsIgnoreCase("games") || strAppCatType.equalsIgnoreCase("videos")
                || strAppCatType.equalsIgnoreCase("themes") || strAppCatType.equalsIgnoreCase("polytones")
                || strAppCatType.equalsIgnoreCase("truetones")) {
            nav_Extra = strAppCatType;
        }
        
        if(app.equalsIgnoreCase("Telenor_Content")) {
            showTopLinks = true;
        }
        
        try{intPageID = Integer.parseInt( request.getParameter("pid"/*page id*/).trim() );}catch(Exception ex) {intPageID = 0;}
        try{intBlock = Integer.parseInt( request.getParameter("bid"/*block id*/).trim() );}catch(Exception ex) {intBlock = 0;}
        //intNextPrBlock = intBlock+1 ;
        if(request.getParameter("check")!=null && request.getParameter("check").equalsIgnoreCase("prv")) {
            if( intBlock > 0 ) {
                intPrvBlock = intBlock-1;
                intPrevousPage =intBlock*intBlockLength - intBlockLength;
                intNextBlock =intBlock;
                intNextPage= intBlock*intBlockLength + intBlockLength;
            }
            else {
                intNextBlock =intBlock+1;
                intNextPage= intBlock*intBlockLength + intBlockLength;
            }
        }
        else if(request.getParameter("check")!=null && request.getParameter("check").equalsIgnoreCase("nxt")) {
            if( intBlock > 0 ) {
                intPrvBlock = intBlock-1;
            }
            else {
                intPrvBlock = intBlock;
            }
            intNextBlock = intBlock+1 ;
            intNextPage= intNextBlock*intBlockLength;
            intPrevousPage =intBlock*intBlockLength - intBlockLength;
        }
        else {
            intNextBlock = intBlock+1;
            if( intBlock > 0 ) {
                intPrvBlock = intBlock-1;
                intPrevousPage =intBlock*intBlockLength - intBlockLength;
            }
            else {
                intPrvBlock = intBlock;
            }
            intNextPage= intBlock*intBlockLength + intBlockLength;
        }
    }
    catch( Exception e ) {
        out.println("ooops! an error occured while processing your request.<br>Please try again.");
        return;
    }
    
String strThisPageName="ContentPage.jsp";
String strPageTitle_thisCat=strTitle;
%>
<%@ include file="../includes/navigationLinks.jspf" %>
<%
    // As Ufone Requirement Create SubCategory or Ringtones are Truetones and Polytones
    
    if("Truetones".equalsIgnoreCase(strAppCatType) || "polytones".equalsIgnoreCase(strAppCatType)){
%>
<div align="center">
        <table width="100%" align="center" cellpadding="0" cellspacing="0" style="text-align: center;font-weight: bold;">
            <tr>
                <td style="background-image:url('../images/new/top_bar_02.jpg');background-repeat: repeat-x;">
                    <a href="<%=context_Path%>/content/ContentPage.jsp?app=<%=app%>&CatID=4&CatType=TRUETONES&MainCat=<%=strAppMainCat%>">Truetones</a>
                </td>
                <td style="background-image:url('../images/new/top_bar_02.jpg');background-repeat: repeat-x;">
                    <img alt="" src="../images/new/top_bar_02_seperator.jpg">
                </td>
                <td style="background-image:url('../images/new/top_bar_02.jpg');background-repeat: repeat-x;">
                    <a href="<%=context_Path%>/content/ContentPage.jsp?app=<%=app%>&CatID=5&CatType=POLYTONES&MainCat=<%=strAppMainCat%>">Polytones</a>
                </td>
            </tr>
            </table>
</div>
<%
}
%>
<div id="contentWrapper" class="contentWrapper">
<%
if(showTopLinks) {
%>
<%--@ include file="../includes/downloadCategories.jspf" --%>
<%
}

//--String tags = "";

tags = request.getParameter("tags") != null ? request.getParameter("tags").toString() : "telenor";
boolean istagged = tags.length() > 0 ? true : false;





String queryToBeExecuted = ""; // Added BY Usman

hmParams = new HashMap<String, String>();


    
        queryToBeExecuted = "CountContent";

        hmParams.put("SingerOrFilm", strAppFOrS);
        hmParams.put("CatType", strAppCatType);
        hmParams.put("MainCat", strAppMainCat);
        hmParams.put("Tags", "");

     

    v = wpb.execCustomQuery(queryToBeExecuted, hmParams);

    Vector rowCount =null;
    try {
        rowCount =(Vector)v.get(0);
    }
    catch(Exception exVectZeroGet){}
    
    if(rowCount!=null) intCountCont = Integer.parseInt(rowCount.get(0).toString());
    else intCountCont=0;



if( intCountCont < intNextPage * intContentLength + 1 ) {
    intNextBlock = intBlock;
    isNext = false;
} else {
    isNext = true;
}
int intPageStartRec = intPageID * intContentLength + 1;
int intPageEndRec = (intPageID + 1) * intContentLength;

hmParams.clear();
hmParams.put("Top", String.valueOf(intPageEndRec));

try {

    
        queryToBeExecuted = "GetContent";
        
        int cp_minLimit = 0;
        cp_minLimit = intPageEndRec - intContentLength;
        if(cp_minLimit < 1) cp_minLimit = 0;
        
        hmParams.put("Top", String.valueOf(cp_minLimit)+","+String.valueOf(intContentLength));

        hmParams.put("FilmOrSinger", strAppFOrS);
        hmParams.put("CatType",strAppCatType);
        hmParams.put("MainCat", strAppMainCat);
        hmParams.put("Tags", "");
        hmParams.put("DYNAMIC_ORDER_CLAUSE", "ContentSequence DESC, ");
        
           
        
    
    v = wpb.execCustomQuery(queryToBeExecuted, hmParams);

    int intRowNumber = 1;
    int intRecordNumber=0;
    Vector row = null;
    boolean bHasImg = false;
    boolean bHasServiceUrl = false;
    boolean bHasJsp = false;
    boolean bHasHtml = false;
    String strTxt= "";
    String strImg = "";
    String strPr = "";

    for(Iterator it = v.iterator();it.hasNext();) {
        row = (Vector)it.next();
        intRecordNumber++;
        if( intRecordNumber < 0 ) { continue; }
        if( intRecordNumber > intPageEndRec ) { break; }

        bHasImg = row.get(2) != null && row.get(2).toString().length() > 3;
        bHasServiceUrl = row.get(4) != null && row.get(4).toString().length() > 5;//service url
        bHasJsp = row.get(5) != null && row.get(5).toString().length() > 5;//jsp column
        bHasHtml = row.get(6) != null && row.get(6).toString().length() > 5;//html column
        strCatID = row.get(7) != null ? row.get(7).toString() : strCatID;//category id

        if(!bHasImg) { strTxt = row.get(1).toString(); }
        else { strImg = row.get(2).toString(); }

        if(row.get(3)!=null) { strPr = row.get(3).toString(); }
%>
        <a class="LinkIndex" href="dload.jsp?page=<%=thisPage%>&app=<%=app%>&cntpg=1&CatID=<%=strCatID%>&cid=<%=row.get(0).toString()%>&bid=<%=intBlock%>&pid=<%=intPageID%>&CatType=<%=strAppCatType%>&MainCat=<%=strAppMainCat%>&FOrS=<%=strAppFOrS%>">
<%
        if(!bHasImg) {
%>
            <%=strTxt%>
        </a><br/>
<%
        }else {
%>
            <img src='<%=context_Path%>/imgsrv?img=<%=strImg%>&repo=test&s=0&w=<%=iconSize%>' alt=''  />
            </a>
<%
        }
    }
}
catch(Exception ex) {
    log.error(ex);
    //out.print(ex.toString());
}

%>
</div>

<%
if(intCountCont > (1)*intContentLength) {
%>
<div class="divStepFoot" width="<%=(int)(wurfl_screenWidth*0.95)%>" >Pages&nbsp;&nbsp;
<%
}
if(intBlock != 0) {
%>
    <a class="LinkFoot" href="ContentPage.jsp?page=<%=thisPage%>&app=<%=app%>&CatType=<%=strAppCatType%>&MainCat=<%=strAppMainCat%>&FOrS=<%=strAppFOrS%>&CatID=<%=strCatID%>&pid=<%=intPrevousPage %>&bid=<%=intPrvBlock%>&tags=&check=prv<%=istagged?"&tags="+tags:""%>"><span class="pagingLinks">Previous</span></a>
<%
}
for(int x = (intBlock*intBlockLength+1); x <= (intBlock+1)*intBlockLength ;x++) {
    if(x-1 == intPageID  && intCountCont > (1)*intContentLength) {
        out.print("<span class='pagingLinks'>"+x+"</span>");
    }
    else {
        if( intCountCont > (x-1)*intContentLength && intCountCont>intContentLength) {
%>
    <a class="LinkFoot" href="ContentPage.jsp?page=<%=thisPage%>&app=<%=app%>&CatType=<%=strAppCatType%>&MainCat=<%=strAppMainCat%>&FOrS=<%=strAppFOrS%>&CatID=<%=strCatID%>&pid=<%=x-1%>&tags=&bid=<%=intBlock%><%=istagged?"&tags="+tags:""%>"><%=x%></a>
<%
        }
    }
}
if(isNext) {
%>
    <a class="LinkFoot" href="ContentPage.jsp?page=<%=thisPage%>&app=<%=app %>&CatType=<%=strAppCatType%>&MainCat=<%=strAppMainCat%>&FOrS=<%=strAppFOrS%>&CatID=<%=strCatID%>&pid=<%=intNextPage %>&tags=&bid=<%=intNextBlock%>&check=nxt<%=istagged?"&tags="+tags:""%>">Next</a>
<%
}
if(intCountCont > (1)*intContentLength) {
%>
</div>
<%
}
%>
<br/>
<div style="padding:0px 0px 0px 0px;text-decoration: underline;">
        <jsp:include page="../banner.jsp">
            <jsp:param name="w" value="<%=width%>"/>
        </jsp:include>
    </div>
<%@ include file="../includes/footer.jspf" %>

<%
    
// prepare to log
logtag = app +" - "+ strAppCatType;
%>


<%
wpb.LogVisit(strIP, thisPage, strGsm, strApn, strBrand, strModel, logtag, request.getQueryString());
%>